package gbench.appdemo.myworld.junit;

import static gbench.appdemo.myworld.market.commodity.Commodity.COMMODITY;
import static gbench.common.tree.LittleTree.Tuple2.TUP2;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import gbench.appdemo.myworld.market.SimpleMarket;
import gbench.appdemo.myworld.market.commodity.Commodity;
import gbench.common.tree.LittleTree.Tuple2;

import java.util.stream.*;

public class JunitTradeBroker {
    
    final Commodity grain = COMMODITY("粮食", "公斤");
    final Commodity wheal = COMMODITY("麦子", "公斤");
    final Commodity rmb = COMMODITY("RMB", "元");
    final Commodity labor = COMMODITY("产业劳动", "天");
    final Commodity bread = COMMODITY("面包", "个");
    
    /**
     * 获得商品列表
     * 
     * @return
     */
    public List<Commodity> commodities() {
        return Arrays.stream(this.getClass().getDeclaredFields())
        .filter(e -> Commodity.class.isAssignableFrom(e.getType())).map(e -> {
            try {
                final var value = (Commodity) e.get(this);
                return value;
            } catch (Exception e1) {
                e1.printStackTrace();
            } // try
            return null;
        }).filter(e -> e != null).collect(Collectors.toList());
    }
    
    @Test
    public void foo() {
        final var broker = SimpleMarket.of("第一产业", grain)
            .getTradeBroker(TUP2("张三",rmb),TUP2("农民",wheal),TUP2("稻田",wheal),TUP2("厨师",bread));// 制作一个交易代理。
        broker.purchase(wheal, 1000, 3000);
        broker.getDoubleEntries().forEach(e->{
            System.out.println(e);
        });
        
        System.out.println("\ntrial");
        broker.trialBalance().forEach(e->{
            System.out.println(e);
        });
    }
    
    /**
     * 电商平台
     * 
     * 这是一个简单的商业系统的演示
     * 1）搜集一些商品
     * 2）把商品投入市场：获取一些列的 商品经营许可
     * 3）把商品经营许可  委托给一个 商品经纪 进行 打理
     * 4）商品经纪 进行商品 买卖 
     * 5）商品经纪 提供财务 状况
     * 
     * 商品经纪 就是 我们 在 电子商务网站上的  账号。 每个电子商务网站就是 一个  商品市场。
     */
    @Test
    @SuppressWarnings("unchecked")
    public void ecommerce() {
        final var cc = commodities().toArray(Commodity[]::new);
        final var n = cc.length;
        final List<Tuple2<String,Commodity>> tups = Stream.iterate(0, i->i+1).limit(100)
                .map(i->TUP2(i+"",cc[i%n]))
                .collect(Collectors.toList());
        tups.add(TUP2("张三",rmb));
        final Tuple2<String,Commodity>[] tuples = tups.toArray(Tuple2[]::new);
        final var broker = SimpleMarket.of("第一产业", grain).getTradeBroker(tuples);// 制作一个交易代理。
        broker.setCurrent("张三").purchase(grain, 1000, 50000);// 采购一批谷物
        broker.purchase(wheal, 1000, 50000);// 采购一批谷物
        broker.purchase(bread, 300, 600);// 采购一批谷物
        broker.getAnyProvider(grain).ifPresent(license->{
            broker.freeze(license, grain, 2000,1000);
            System.out.println(license);
        });
        System.out.println("张三的经营状况:");
        broker.trialBalance().forEach(e->{
            System.out.println(e);
        });
    }

}
